/**
 * 
 */
package org.jft.datamodel.database.mapping;

import java.io.IOException;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.HibernateException;
import org.hibernate.annotations.GenericGenerator;
import org.jft.datamodel.database.dao.DAOFactory;

/**
 * @author LAP
 *
 */
@Entity
@Table(name = "couple")
public class Couple {
	@Id
	@GeneratedValue(generator = "Increment")
	@GenericGenerator(name = "Increment", strategy = "Increment")
	@Column(name = "couple_id", nullable = false, unique = true)
	private Long coupleId;
	
	@Column(name = "first_person_id", nullable = false)
	private Long firstPersonId;
	
	@Column(name = "second_person_id", nullable = false)
	private Long secodPersonId;
	
	@ManyToOne
	@JoinColumn(name = "first_person_id")
	private Person firstPerson;
	
	@ManyToOne
	@JoinColumn(name = "second_person_id")
	private Person secondPerson;
	
	@OneToMany(mappedBy = "couple")
	private List<Parents> descendants;
	
	@ManyToMany
	@JoinTable(
			name = "couple_state",
			joinColumns = {@JoinColumn(name = "couple_id", referencedColumnName = "couple_id")},
			inverseJoinColumns = {@JoinColumn(name = "state_id", referencedColumnName = "state_id")})
	private List<State> states;
	
	@ManyToMany
	@JoinTable(
			name = "couple_events",
			joinColumns = {@JoinColumn(name = "couple_id", referencedColumnName = "couple_id")},
			inverseJoinColumns = {@JoinColumn(name = "event_id", referencedColumnName = "event_id")})
	private List<Event> events;

	public Long getId() {
		return coupleId;
	}

	public void setId(Long value) {
		coupleId = value;
	}

	public Long getFirstPersonId() {
		return firstPersonId;
	}

	public void setFirstPersonId(Long value) {
		firstPersonId = value;
	}

	public Long getSecodPersonId() {
		return secodPersonId;
	}

	public void setSecodPersonId(Long value) {
		secodPersonId = value;
	}

	public Person getFirstPerson() {
		return firstPerson;
	}

	public void setFirstPerson(Person value) {
		firstPerson = value;
		setFirstPersonId(value.getID());
	}

	public Person getSecondPerson() {
		return secondPerson;
	}

	public void setSecondPerson(Person value) {
		secondPerson = value;
		setSecodPersonId(value.getID());
	}

	public List<Parents> getDescendants() {
		return descendants;
	}

	public void addDescendant(Person value) throws HibernateException, IOException {
		DAOFactory.getinstance().getParentsDAO().insertDescendant(value, this);
	}
	
	public void removeDescendant(Person value) throws HibernateException, IOException {
		DAOFactory.getinstance().getParentsDAO().deleteDescendant(value);
	}
}
